package com.hj.ct.logic;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hj.ct.App;
import com.hj.ct.http.RcsHttpUtil;

public class BaseLogic {
	protected RcsHttpUtil httpUtil = RcsHttpUtil.getInstance(App.getContext());

	public JSONObject parseJson(String json) throws JSONException {
		JSONObject jobj = new JSONObject(json);
		try {
			long ts = jobj.optLong("ts") * 1000;
			LoginLogic.getInstance().setServerTime(ts);
		} catch (Exception e) {

		}
		return jobj;
	}

	public interface OnHandlerResponse {
		public void handlerResponse(int action, int code, Object... obj);
	}

	public String makeParamts(ContentValues values) {
		StringBuffer buffer = new StringBuffer();
		Set<String> set = values.keySet();
		for (String key : set) {
			buffer.append("&" + key + "=" + values.getAsString(key));
		}
		return buffer.toString();
	}

	protected <T> T parseJson(String json, Class<T> classOfT) {
		T t = null;
		try {
			t = new Gson().fromJson(json, classOfT);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return t;
	}

	protected <T> List<T> parseJsonArray(String jsonString, Class<T> classOfT) {
		List<T> list = new ArrayList<T>();
		try {
			list = new Gson().fromJson(jsonString, new TypeToken<List<T>>() {
			}.getType());
		} catch (Exception e) {
			list = null;
		}
		return list;
	}

}
